<?php
set_time_limit(0);

include_once (dirname(__FILE__).'/config/database.php');
include_once (dirname(__FILE__).'/class/Log.inc');
include_once (dirname(__FILE__).'/class/Mysql/Exception.inc');
include_once (dirname(__FILE__).'/class/Mysql.inc');

define('LOCK_FILE', dirname(__FILE__).'/lock/unbannedDaemon.lock');
define('LOG_LEVEL', 'DEBUG');

$LogFile = dirname(__FILE__).'/log/unbannedDaemon'.date('Y.m.d').'.log';

$Log = new Zw_Log();
$Log->setCurrLogLevel(LOG_LEVEL);
$Log->setLogFile($LogFile);

$handle = fopen(LOCK_FILE, 'w+');
if(!flock($handle, LOCK_EX|LOCK_NB)){
	$Log->write('进程已经启动，请不要重复启动');
	exit();
}

$GmDb = new Zw_Mysql();
$GmDb->setLog($Log);
$GmDb->connect($gmDatabaseHost, $gmDatabaseUsername, $gmDatabasePassword, $gmDatabaseName);

$GameDb = new Zw_Mysql();
$GameDb->setLog($Log);

while (1){
	$LogFile = dirname(__FILE__).'/log/unbannedDaemon'.date('Y.m.d').'.log';
	$Log->setLogFile($LogFile);
	
	try {
		$sql = "
			select
				a.`id`,
				d.`ipAddress`,
				b.`databaseName`,
				a.`userId`
			from
				`t_log_banned_role` a
			left join
				`t_data_game_area` b
			on
				a.`gameAreaId` = b.`gameAreaId`
			left join
				`t_data_server_ip` c
			on
				b.`serverId` = c.`serverId`
			left join
				`t_data_ip` d
			on
				c.`ipId` = d.`ipId`
			left join
				`t_cfg_ip_type` e
			on
				d.`ipTypeId` = e.`ipTypeId`
				and
				e.`isDefault` = 1
			where
				a.`type` = 1
				and
				a.`status` = 1
				and
				a.`unbannedDateTime` <= now()
		";
		$unbannedInfoArray = $GmDb->getArray($sql);
		
		foreach ($unbannedInfoArray as $v) {
			$GameDb->connect($v['ipAddress'], $gameDatabaseUsername, $gameDatabasePassword, $v['databaseName']);
			
			$sql = "
				update
					`t_user` a
				left join
					`t_char` b
				on
					a.`userid` = b.`uid`
				set
					b.`enable` = '1'
				where
					a.`userid` = '{$v['userId']}'
					and
					b.`enable` = '0'
			";
			$GameDb->query($sql);
			$GameDb->close();
			
			$sql = "
				update
					`t_log_banned_role`
				set
					`status` = 2
				where
					`id` = '{$v['id']}'
			";
			$GmDb->query($sql);
		}
		
		unset($unbannedInfoArray);
		
		$sql = "
			select
				a.`id`,
				d.`ipAddress`,
				b.`databaseName`,
				a.`userId`
			from
				`t_log_banned_chat` a
			left join
				`t_data_game_area` b
			on
				a.`gameAreaId` = b.`gameAreaId`
			left join
				`t_data_server_ip` c
			on
				b.`serverId` = c.`serverId`
			left join
				`t_data_ip` d
			on
				c.`ipId` = d.`ipId`
			left join
				`t_cfg_ip_type` e
			on
				d.`ipTypeId` = e.`ipTypeId`
				and
				e.`isDefault` = 1
			where
				a.`type` = 1
				and
				a.`status` = 1
				and
				a.`unbannedDateTime` <= now()
		";
		$unbannedInfoArray = $GmDb->getArray($sql);
		
		foreach ($unbannedInfoArray as $v) {
			$GameDb->connect($v['ipAddress'], $gameDatabaseUsername, $gameDatabasePassword, $v['databaseName']);
				
			$sql = "
				update
					`t_user_status`
				set
					`talkbanned` = 0
				where
					`userId` = '{$v['userId']}'
			";
			$GameDb->query($sql);
			$GameDb->close();
				
			$sql = "
				update
					`t_log_banned_chat`
				set
					`status` = 2
				where
					`id` = '{$v['id']}'
			";
			$GmDb->query($sql);
		}
		
		unset($unbannedInfoArray);
	}
	
	catch(Zw_Mysql_Exception $e) {
		$Log->write('数据库错误', 'ERROR');
	}
	catch(Exception $e) {
		$Log->write('系统错误', 'ERROR');
	}
	
	sleep(30);
}